﻿using System;
using System.IO;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Vertex.Abstractions.Storage;
using Vertex.Storage.Linq2db.Core;
using Vertex.Storage.Linq2db.Db;
using Vertex.Storage.Linq2db.Options;
using Vertex.Storage.Linq2db.Storage;
using Vertex.Transaction.Abstractions.Storage;

namespace Vertex.Storage.Linq2db
{
    public static class Extensions
    {
        public static void AddLinq2DbStorage(this IServiceCollection serviceCollection, Action<DbPoolOptions> configAction, params EventArchivePolicy[] archivePolicies)
        {
            serviceCollection.Configure<DbPoolOptions>(config => configAction(config));
            serviceCollection.AddSingleton<IEventArchiveFactory, EventArchiveFactory>();
            serviceCollection.AddSingleton<IEventStorageFactory, EventStorageFactory>();
            serviceCollection.AddSingleton<ISnapshotStorageFactory, SnapshotStorageFactory>();
            serviceCollection.AddSingleton<ISubSnapshotStorageFactory, SubSnapshotStorageFactory>();
            serviceCollection.AddSingleton<ITxEventStorageFactory, TxEventStorageFactory>();
            serviceCollection.AddSingleton<DbFactory>();
            var eventArchiveContainer = new EventArchivePolicyContainer();
            foreach (var policy in archivePolicies)
            {
                eventArchiveContainer.Container.Add(policy.Name, policy);
            }
            serviceCollection.AddSingleton(eventArchiveContainer);
        }

        public static void AddTestLinq2DbStorage(this IServiceCollection serviceCollection)
        {
            //var sqliteDbPath = @"C:\tzm工作\OpenSource\dynamic-system-design\DSD.ES\ES.Server\bin\Debug\net5.0\Vertex.SQLite.db";

            var sqliteDbPath = @"D:\Devops\dynamic-system-design\DSD.ES\ES.Server\bin\Debug\net5.0\Vertex.SQLite.db";
            if (!File.Exists(sqliteDbPath))
            {
                throw new Exception("数据库文件不存在");
            }
            
            Action<DbPoolOptions> configAction = config =>
                        {
                            config.Connections = new Vertex.Storage.Linq2db.Options.ConnectionOptions[]
                            {
                         new ConnectionOptions
                         {
                            Name = "project",
                            ProviderName = "SQLite.MS",
                            ConnectionString = $"Data Source={sqliteDbPath};"
                         }
                            };
                        };

            var policy = new EventArchivePolicy("month", (name, time) => $"Vertex_Archive_{name}_{DateTimeOffset.FromUnixTimeSeconds(time).ToString("yyyyMM")}".ToLower(), table => table.StartsWith("Vertex_Archive".ToLower()));

            serviceCollection.Configure<DbPoolOptions>(config => configAction(config));
            serviceCollection.AddSingleton<IEventArchiveFactory, EventArchiveFactory>();
            serviceCollection.AddSingleton<IEventStorageFactory, EventStorageFactory>();
            serviceCollection.AddSingleton<ISnapshotStorageFactory, SnapshotStorageFactory>();
            serviceCollection.AddSingleton<ISubSnapshotStorageFactory, SubSnapshotStorageFactory>();
            serviceCollection.AddSingleton<ITxEventStorageFactory, TxEventStorageFactory>();
            serviceCollection.AddSingleton<DbFactory>();
            var eventArchiveContainer = new EventArchivePolicyContainer();
            eventArchiveContainer.Container.Add(policy.Name, policy);
            serviceCollection.AddSingleton(eventArchiveContainer);
        }



    }
}
